'use strict';
//////////////////////
// 引入所有样式库
// require('@/css/index.css');
require('font-awesome/css/font-awesome.min.css');
require('simple-line-icons/css/simple-line-icons.css');
require('@/vender/datetimepicker/bootstrap.datetimepicker.min.css');
// 定制化样式库
require('@/css/bootstrap.css');
require('@/css/style.css');
require('@/css/layout.css');
require('@/css/lingsui.css');
require('@/css/xian.css');
// 引入所有大图片
require('@/images/icon.sword.cur');
require('@/images/logo.pro.png');
require('@/images/logo.Conpany.png');
require('@/images/logo.xian.png');
require('@/images/ad.duilian.jpg');
require('@/images/head.zhishui.jpg');
require('@/images/head.you.jpg');
require('@/images/splite.1.png');
require('@/images/splite.4.png');
require('@/images/splite.menu_c.png');
require('@/images/splite.menu_r.png');
require('@/images/border.1.png');
require('@/images/border.2.png');
require('@/images/loading.6.gif');
require('@/images/img.404.jpg');
require('@/images/img.addpic.jpg');
require('@/images/img.upfile.png');
require('@/images/lide/Group-29@2x.png');
require('@/images/lide/icon_close2@2x.png');
require('@/images/lide/sprite1.png');
require('@/images/lide/sprite2.png');
// 多媒体
require('@/audio/chime.mp3');
require('@/audio/chime.ogg');
require('select2')
require('jquery-sparkline')
require('./vender/ledate/datepicker')
require('./vender/ledate/datepicker.css')
require('./vender/city-picker/js/city-picker.min.js')
require('./vender/city-picker/css/city-picker.css')
require('./vender/datetimepicker/bootstrap.datetimepicker.min.js')
// 引入全局依赖库
var mock = require('mockjs')
window.Mock = mock
require('google-code-prettify/bin/prettify.min.js')
// 引库
var _ = require('lodash');
// 引入路由
var router = require('./router');
// ng依赖注入
var app = angular.module('wabgApp', [
    'ngAnimate',// angular-animate.min.js
    'ngResource',// angular-resource.min.js
    'ngSanitize',// angular-sanitize.min.js
    'ngCookies',// angular-cookies.min.js
    'ngRoute',// angular-route.min.js
    'ui.router',// router.js
    'ui.bootstrap',// bootstrap.tpls.0.12.0.js
    'ui.validate',// validate.min.js
    'wabgApp.directives',
    'wabgApp.provider',
    'wabgApp.factory',
    'wabgApp.services',
    'wabgApp.filter',
    router.default
]);
// 路由注册
app.config(function($stateProvider, $urlRouterProvider){
    // 默认路由
    $urlRouterProvider.otherwise('app/main');
    for(var i = 0, l = router.routeList.length; i < l; i++){
        var routeItem = {};
        var one = router.routeList[i];
        routeItem.name = one.name;
        routeItem.url = one.url || '/' + one.name.replace(/.*\./g, '');
        routeItem.params = one.params;
        if( one.templateUrl ){ routeItem.templateUrl = one.templateUrl }
        if( one.template ){ routeItem.template = one.template }
        // 绝妙的想法
        if( one.controllerName ){ routeItem.controller = one.controllerName }
        else{ routeItem.controller = function(){} }
        $stateProvider.state(routeItem.name, routeItem)
    }
});
//////////////////////////////////////////////////////////////////////////////
// AJAX异步请求全局配置
// 可以在这里指定每次的ajax都携带关键信息
// 为了解决angular post 提交时候 参数默认json格式 下面12两条
// =====================
// liuyp 2018年11月28日15:30:04
//////////////////////////////////////////////////////////////////////////////
app.config(['$httpProvider', function( $httpProvider ) {
    if ( ! $httpProvider.defaults.headers.get ) {
        $httpProvider.defaults.headers.get = {}
    }
    $httpProvider.defaults.headers.common = {
        'X-Requested-With': 'XMLHttpRequest'
    }
    $httpProvider.defaults.headers.post = {
        // 1 就是这一句坑了 所有的post提交都是json格式的 这个是声明给服务器看的
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        // 'Content-Type': 'application/json; charset=UTF-8',
        'author': 'mr.xian',
        'Accept': 'application/json, text/javascript,*; q=0.01'
    }
    // 重写angular的param方法，使angular使用jquery一样的数据序列化方式
    // @param {Object} obj
    // @return {String}
    var param = function (obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for (name in obj) {
            value = obj[name];
            if (value instanceof Array) {
                for (i = 0; i < value.length; ++i) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value instanceof Object) {
                for (subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
        return query.length ? query.substr(0, query.length - 1) : query;
    };
    // 2 自己传递的格式变成url格式
    $httpProvider.defaults.transformRequest = [function (data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
    $httpProvider.defaults.headers.get = {
        'Cache-Control': 'no-cache',
        'Pragma': 'no-cache'
    }
}])
// 主控制器
app.controller('mainController', function ($scope, $rootScope, $system, $ctrlCache, $state) {
    // 拉起帷幕
    $system.closeLoading()
    // 路由相关
    // 路由切换开始 无效
    /* $scope.$on('$routeChangeStart', function(a, b) {
        console.log($rootScope.$$ChildScope())
        console.info('仙', '路由切换开始')
    }) */
    // 路由切换结束
    $scope.$on('$viewContentLoaded', _.debounce(function () {
        var name = $state.current.name
        if (name) {console.info('仙', '路由切换, 进入页面', name)}
        $ctrlCache.routeRemove();
        var doc = document.body.scrollTop? document.body : document.documentElement
        doc.scrollTop = 0
    }, 2e2) )
    // 跳转的方便写法
    $rootScope.goto = function(name, params){
        $state.go(name, params, {reload:true})
    }
    // 面包屑标题
    $scope.breadcrumbTitle = '';
    // 监听广播
    $scope.$on('changeBreadcrumbTitle',function(e, data) {
        $scope.breadcrumbTitle = data.breadcrumb;
    })
});
// 空控制器
app.controller('nullController', function ($scope) {
});
// 头部控制器
app.controller('headerController', function ($scope) {
});
// 树形目录控制器
app.controller('menuController', function ($scope, $rootScope, $state) {
    //树型结构 判断自己有没有被选中
    $scope.isMenuSelectMe = function(name){
        var cname = $state.current.name;
        // app.ng.uirouter 选中 app.ng
        // app.ng.uirouter 选中 app.ng.uirouter
        // app.ng.uiroutersub 选中 app.ng.uirouter
        if(cname.indexOf('&')>-1){ // 兄弟元素
            cname = cname.split('&')[0]
        }
        return cname.indexOf(name)>-1 //子包含父
            || name == cname // 相等
            || name.indexOf(cname) > -1 // 父包含子
    };
    // 循环制作左侧目录
    var menuconfig = '',
        menulist = [], // 循环用*/
        two = {}, // 对比用缓存上一个节点*/
        bro = false //一群兄弟节点只有老大可升级显示*/
        ;
    menuconfig = router.routeList;
    for (var i = 0,l = menuconfig.length; i < l; i++) {
        // 设计的只支持两层
        var one = menuconfig[i]
        if(bro && one.sleaf !== undefined){
            one.sleaf = undefined
        }else{
            bro = false
        }
        if(one.tree){
            two = { page: one.page, tree: one.tree,title: one.title || '节点', sub: [], name: one.name, icon: one.icon }
            menulist.push(two)
        }else if (one.sleaf !== undefined) {
            if($power.get(one.bypower)) continue;
            if(!two.first) two.first = one.name;
            two.sub.push({ leaf: one.leaf,title: one.title || '节点', name: one.name, pname: one.pname })
            bro = true
        }else if (one.leaf && one.leaf == two.tree) {
            if(!two.first) two.first = one.name;
            two.sub.push({ leaf: one.leaf,title: one.title || '节点', name: one.name, pname: one.pname })
        }
        // console.info('注册目录',one)
    }
    $rootScope.menulist = menulist;
});
// 手动加载angularjs模板
angular.bootstrap(document,['wabgApp']);
// 记录
console.info('仙', 'app.js 执行完毕')